﻿using System;
using System.Windows.Data;

namespace XmlVisualizer {
	/// <summary>
	/// Selected language converter.
	/// </summary>
	public class SelectedLangConverter : IValueConverter {
		#region IValueConverter Members

		/// <summary>
		/// Returns true if value == parameter, otherwise returns false.
		/// </summary>
		/// <param name="value">value to convert</param>
		/// <param name="targetType">target type</param>
		/// <param name="parameter">conversion parameter</param>
		/// <param name="culture">CultureInfo</param>
		/// <returns>True if value == parameter, false otherwise.</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			//if (LanguageContext.Instance.Culture.Name == value as String) {
			if (value as String == parameter as String) {
				return true;
			} else {
				return false;
			}
		}

		/// <summary>
		/// Returns null.
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>null.</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			return null;
		}

		#endregion
	}

	/// <summary>
	/// Converter that converts between zoom ratio (double) and its percentage string representation.
	/// </summary>
	public class ZoomConverter : IValueConverter {
		#region IValueConverter Members

		/// <summary>
		/// Converts from double to percentage string.
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is double) {
				double newValue = (double)value * 100;
				return newValue + "%";
			} else {
				return value;
			}
		}

		/// <summary>
		/// Converts from percentage string back to double.
		/// </summary>
		/// <param name="value"></param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns></returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
			if (value is string) {
				if (String.IsNullOrEmpty(value as String)) {
					return 0.1;
				} else {
					double val = 0.1;
					try {
						val = double.Parse(value as string);
					} catch (FormatException) {

					}
					return val / 100;
				}
			} else {
				return value;
			}
		}

		#endregion
	}
}